//
//  SYDSetPaymentPasswordController.m
//  PinGJByOwnerSide
//
//  Created by lantaiyuan on 2017/8/11.
//  Copyright © 2017年 lantaiyuan. All rights reserved.
//

#import "SYDSetPaymentPasswordController.h"

@interface SYDSetPaymentPasswordController ()<UITextFieldDelegate>
@property(nonatomic,strong)UILabel *titleLabel;
@property (nonatomic, strong) SYPasswordView *pasView;
@property(nonatomic,strong)UIButton *sureBtn;
@property(nonatomic,strong)UILabel *showLabel;

@end

@implementation SYDSetPaymentPasswordController
#pragma mark - 导航栏
- (void)navigationBarHandle{
    
    // 导航栏返回按钮
    WEAKSELF
    [self backButtonAction:^{
        
        [weakSelf.navigationController popViewControllerAnimated:YES];
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationBar.title = @"设置支付密码";
    [self navigationBarHandle];
    [self initSetPaymentUI];
    
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    
    [center addObserver:self selector:@selector(receivePaymentPasswordNotificiation:) name:@"PaymentPassword" object:nil];
    [center addObserver:self selector:@selector(receiveConfirmPassworddNotificiation:) name:@"confirmPassword" object:nil];
}
-(void)initSetPaymentUI{
    
    self.titleLabel = [[UILabel alloc]init];
    [self.view addSubview:self.titleLabel];
    if ([[YMSaveTool valueForKey:@"paymentWord"]isEqualToString:@""]) {
        self.titleLabel.text = @"请设置支付秘密,建议勿与银行卡取款密码相同";
    }else{
      self.titleLabel.text = @"请再次输入以确认密码";
    }
    self.titleLabel.textColor = SUB_TITLE_COLOR;
    self.titleLabel.font = [UIFont systemFontOfSize:14];
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    self.titleLabel.numberOfLines = 0;
    
    //字符密码
    self.pasView = [[SYPasswordView alloc] initWithFrame:CGRectMake(15, 133, SCREEN_WIDTH - 30, 44)];
    [self.view addSubview:self.pasView];

    //确认按钮
    self.sureBtn = [[UIButton alloc]init];
    self.sureBtn.backgroundColor = GREEN_COLOR;
    [self.sureBtn drawBorderWithColor:GREEN_COLOR radius:5];
    [self.sureBtn setTitle:@"完成" forState:UIControlStateNormal];
    [self.sureBtn setTitleColor:WHITE_COLOR forState:UIControlStateNormal];
    #pragma mark---约束
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(14));
        make.centerX.equalTo(self.view.mas_centerX);
        make.top.equalTo(self.view.mas_top).offset(99);
        make.left.equalTo(self.view.mas_left).offset(15);
        make.right.equalTo(self.view.mas_right).offset(-15);
        
    }];
}
-(void)showLabelUI{
    
    [UIView animateWithDuration:2.0 animations:^{
        self.showLabel = [[UILabel alloc]init];
        [self.view addSubview:self.showLabel];
        self.showLabel.backgroundColor  = [UIColor colorWithHexString:@"#03111e"];
        self.showLabel.alpha  = 0.5;
        [self.showLabel drawBorderWithColor:[UIColor colorWithHexString:@"#03111e"] radius:4];
        self.showLabel.layer.cornerRadius = 4;
        self.showLabel.layer.masksToBounds = YES;
        self.showLabel.text = @"设置密码成功";
        self.showLabel.textColor  = WHITE_COLOR;
        self.showLabel.font = [UIFont systemFontOfSize:14];
        self.showLabel.textAlignment = NSTextAlignmentCenter;
        [self.showLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(@(33));
            make.bottom.equalTo(self.view.mas_bottom).offset(-265);
            make.width.equalTo(@(120));
            make.centerX.equalTo(self.view.mas_centerX);
        }];
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:2.0 animations:^{
            [self.showLabel removeFromSuperview];
            [self dismissViewControllerAnimated:YES completion:nil];
        }];
    }];
}

//通知中心的回调方法
- (void)receivePaymentPasswordNotificiation:(NSNotification*)sender{
    
    dispatch_async(dispatch_get_main_queue(), ^{
        self.titleLabel.text = @"请再次输入以确认密码";
    });
    
}
//通知中心的回调方法
- (void)receiveConfirmPassworddNotificiation:(NSNotification*)sender{
    
    if ([[YMSaveTool valueForKey:@"confirmPassword"] isEqualToString:[YMSaveTool valueForKey:@"paymentWord"]]) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.view addSubview:self.sureBtn];
            [self.sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.height.equalTo(@(40));
                make.top.equalTo(self.pasView.mas_bottom).offset(50);
                make.left.equalTo(self.view.mas_left).offset(15);
                make.right.equalTo(self.view.mas_right).offset(-15);
                
            }];
            [[self.sureBtn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
                NSLog(@"点击了确认按钮");
                [self showLabelUI];
            }];
        });
    }else{
        [SVProgressHUD displayInfoWithStatus:@"两次输入的密码不符" time:1.5f];
        [self.pasView clearUpPassword];
    }
}
//将通知中心移除

- (void)dealloc{
    
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
